package com.bs.common.utils;

import cn.hutool.core.collection.CollUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author bingshao
 * @date 2023/5/10
 **/
public class CommonUtil {

    /**
     * 后面补0
     *
     * @param number
     * @param length
     * @return
     */
    public static String formatNumber(String number, int length) {
        int strLength = number.length();
        int count = length - strLength;
        for (int i = 0; i < count; i++) {
            number = number + "0";
        }
        return number;
    }

    /**
     * 去除末尾0
     *
     * @param input
     * @return
     */
    public static String removeTrailingZeros(String input) {
        return input.replaceAll("(0)+$", "");
    }


    public static String areaCodeRemoveZeros(String input) {
        String s = input.replaceAll("(0)+$", "");
        if (s.length() == 1) {
            return s + "0";
        }
        if (s.length() == 3) {
            return s + "0";
        }
        if (s.length() == 5) {
            return s + "0";
        }
        return s;
    }

    /**
     * 数组切割
     *
     * @param list
     * @param groupSize
     * @return
     */
    public static <T> List<List<T>> splitListByGroupSize(List<T> list, int groupSize) {
        List<List<T>> resultList = new ArrayList<>();
        if (CollUtil.isEmpty(list)) {
            return resultList;
        }

        int total = list.size();

        if (groupSize >= total) {
            resultList.add(list);
            return resultList;
        } else if (groupSize < 1) {
            //元素个数最少1个
            groupSize = 1;
        }

        int groupCount = total / groupSize + (total % groupSize > 0 ? 1 : 0);

        //把列表按指定大小拆分为多个List
        for (int i = 0; i < groupCount; i++) {
            int start = i * groupSize, end = i == groupCount - 1 ? total : (i + 1) * groupSize;
            resultList.add(list.subList(start, end));
        }

        return resultList;
    }

    public static void main(String[] args) {
        String s = removeTrailingZeros("203000000");
        System.out.println(s);
    }
}
